﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Services;
using Microsoft.Practices.EnterpriseLibrary.Caching;
using Barbar.MapReport.Configuration;
using Barbar.MapReport.Common;
using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations;

namespace Barbar.MapReport.HtmlRender.Facade {
  [WebService(Namespace=Constants.XmlNamespace)]
  public class HtmlRenderService : WebService {
    [WebMethod()]
    [SoapDebugging()]
    public string CreateRenderingContext() {
      string uid = Guid.NewGuid().ToString("N");
      RenderingService service = new RenderingService();
      CacheManager cacheManager = CacheFactory.GetCacheManager();
      cacheManager.Add(uid, service, CacheItemPriority.NotRemovable, null, new SlidingTime(new TimeSpan(0, 20, 0)));
      return uid;
    }

    [WebMethod()]
    [SoapDebugging()]
    public string GetHandlerUrl() {
      return MapConfigurationManager.HandlerUrl;
    }

    [WebMethod()]
    [SoapDebugging()]
    public void Execute(DrawingRequest request) {
      CacheManager cacheManager = CacheFactory.GetCacheManager();
      RenderingService service = (RenderingService)cacheManager[request.ContextId];
      service.Execute(request.Command);
    }

    [WebMethod()]
    [SoapDebugging()]
    public RenderResponse RenderRequest(RenderHtmlFragmentRequest request) {
      CacheManager cacheManager = CacheFactory.GetCacheManager();
      RenderingService service = (RenderingService)cacheManager[request.ContextId];
      MapHtmlFragment fragment = service.Render(request.RenderSettings);
      return request.ProcessResponse(fragment);      
    }
  }
}
